#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using WaveMaster.Holidays.Club.Service;
using WaveMaster.Holidays.Club.Domain;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class SearchPassportForm : Form
    {
        PassportService m_passportService;
        Keys m_currentKey;
        IPassportParentForm m_parentForm;
        public SearchPassportForm(IPassportParentForm parentForm, string text)
        {
            InitializeComponent();
            m_parentForm = parentForm;
            textBoxSearch.Text = text;
            m_passportService = new PassportService();// (PassportService)ServiceFactory.Instance().GetService(ServiceFactory.PASSPORT_SERVICE);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            IList users = m_passportService.SearchPassportByCodeName(textBoxSearch.Text);
            foreach (Passport user in users)
            {
                ListViewItem item = listViewRecords.Items.Add(user.m_passportNo);
                item.SubItems.Add(user.m_issueDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(user.m_expireDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(user.m_name);
                item.SubItems.Add(user.m_country);
                item.SubItems.Add(user.m_birthDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(user.m_birthPlace);
                item.SubItems.Add(user.m_address);
                item.Tag = user;
            }
            if (users.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (users.Count == 0)
            {
                ListViewItem item = listViewRecords.Items.Add("Not Found.");
                textBoxSearch.SelectAll();
            }
        }

        private void textBoxSearch_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
        }

        private void textBoxSearch_GotFocus(object sender, EventArgs e)
        {
            textBoxSearch.SelectAll();
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            if(listViewRecords.SelectedItem==null)return;
            if(listViewRecords.SelectedItem.Tag==null)return;
            Passport p = (Passport)listViewRecords.SelectedItem.Tag;
            m_parentForm.LoadPassport(p);
            this.Close();
            m_parentForm.SetActiveCursor();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            listViewRecords_DoubleClick(sender, e);
        }

        private void listViewRecords_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar== Convert.ToChar(Keys.Enter))
            {
                buttonAdd_Click(sender, e);
            }
        }
    }
}